package com.nutiteq.yahoo;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDletStateChangeException;

import com.nutiteq.MapComponent;
import com.nutiteq.components.WgsPoint;
import com.nutiteq.controls.ControlKeys;
import com.nutiteq.listeners.MapListener;
import com.nutiteq.net.DefaultDownloadStreamOpener;
import com.nutiteq.utils.DummyStarter;
import com.nutiteq.utils.InitializedCanvas;
import com.nutiteq.utils.LogForm;

public class YahooCanvas extends Canvas implements InitializedCanvas, MapListener, CommandListener {
  private final YahooCanvas instance;
  private Image splash;

  private final Timer timer;
  private Thread starter;

  private final MapComponent mapDisplay;
  private boolean initialized;

  private Command exit;

  public YahooCanvas() {
    instance = this;

    try {
      splash = Image.createImage("/splash.png");
    } catch (final IOException e) {
      e.printStackTrace();
    }

    timer = new Timer();
    timer.schedule(new TimerTask() {
      public void run() {
        starter = new Thread(new DummyStarter(YahooSample.instance, instance));
        starter.start();
      }
    }, 3000);
    mapDisplay = new MapComponent("abcdtrial", YahooSample.instance, getWidth(), getHeight(),
        new WgsPoint(24.764580, 59.437420), 10);
  }

  public void initialize() {
    mapDisplay.setMap(new YahooMap());

    final DefaultDownloadStreamOpener streamOpener = new DefaultDownloadStreamOpener();
    streamOpener.addRequestProperty("User-Agent",
        "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");

    mapDisplay.setDownloadStreamOpener(streamOpener);
    mapDisplay.startMapping();

    mapDisplay.setMapListener(this);

    mapDisplay.defineControlKey(ControlKeys.MOVE_UP_KEY, -1);
    mapDisplay.defineControlKey(ControlKeys.MOVE_DOWN_KEY, -2);
    mapDisplay.defineControlKey(ControlKeys.MOVE_LEFT_KEY, -3);
    mapDisplay.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, -4);
    mapDisplay.defineControlKey(ControlKeys.SELECT_KEY, -5);
    mapDisplay.defineControlKey(ControlKeys.ZOOM_OUT_KEY, Canvas.KEY_NUM1);
    mapDisplay.defineControlKey(ControlKeys.ZOOM_IN_KEY, Canvas.KEY_NUM3);
    //MPowerPlayer
    mapDisplay.defineControlKey(ControlKeys.MOVE_UP_KEY, 38);
    mapDisplay.defineControlKey(ControlKeys.MOVE_DOWN_KEY, 40);
    mapDisplay.defineControlKey(ControlKeys.MOVE_LEFT_KEY, 37);
    mapDisplay.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, 39);
    mapDisplay.defineControlKey(ControlKeys.SELECT_KEY, 10);
    //for motorola
    mapDisplay.defineControlKey(ControlKeys.MOVE_UP_KEY, KEY_NUM2);
    mapDisplay.defineControlKey(ControlKeys.MOVE_DOWN_KEY, KEY_NUM8);
    mapDisplay.defineControlKey(ControlKeys.MOVE_LEFT_KEY, KEY_NUM4);
    mapDisplay.defineControlKey(ControlKeys.MOVE_RIGHT_KEY, KEY_NUM6);
    mapDisplay.defineControlKey(ControlKeys.SELECT_KEY, KEY_NUM5);

    mapDisplay.showZoomLevelIndicator(true);

    exit = new Command("Exit", Command.EXIT, 0);

    addCommand(exit);
    addCommand(LogForm.SHOW_LOG);
    setCommandListener(this);

    initialized = true;
    repaint();
  }

  protected void paint(final Graphics g) {
    if (!initialized) {
      //paint splash
      g.setColor(0xFFFFFFFF);
      g.setClip(0, 0, getWidth(), getHeight());
      g.fillRect(0, 0, getWidth(), getHeight());
      g.drawImage(splash, (getWidth() - splash.getWidth()) / 2,
          (getHeight() - splash.getHeight()) / 2, Graphics.TOP | Graphics.LEFT);
      return;
    }

    mapDisplay.paint(g);
  }

  public void mapClicked(final WgsPoint p) {

  }

  public void mapMoved() {
    System.out.println(mapDisplay.getMiddlePoint());
  }

  public void needRepaint(final boolean mapIsComplete) {
    repaint();
  }

  protected void keyPressed(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapDisplay.keyPressed(keyCode);
  }

  protected void keyReleased(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapDisplay.keyReleased(keyCode);
  }

  protected void keyRepeated(final int keyCode) {
    if (!initialized) {
      return;
    }
    mapDisplay.keyRepeated(keyCode);
  }

  protected void pointerDragged(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapDisplay.pointerDragged(x, y);
  }

  protected void pointerPressed(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapDisplay.pointerPressed(x, y);
  }

  protected void pointerReleased(final int x, final int y) {
    if (!initialized) {
      return;
    }
    mapDisplay.pointerReleased(x, y);
  }

  public void commandAction(final Command arg0, final Displayable arg1) {
    if (arg0 == exit) {
      try {
        YahooSample.instance.destroyApp(true);
      } catch (final MIDletStateChangeException ignore) {
      }
    } else if (arg0 == LogForm.SHOW_LOG) {
      Display.getDisplay(YahooSample.instance).setCurrent(
          new LogForm(mapDisplay, YahooSample.instance, this));
    }
  }
}
